//
//  ZTXJieDanBottomView.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/10/19.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXJieDanBottomView.h"
@interface ZTXJieDanBottomView ()
{
    NSString *_dateString;
    /**
     0 非报价单 货源单 独立司机
     1 非报价单 非货源单 独立司机
     2 报价单 货源单 独立司机
     3 报价单 非货源单 独立司机
     4 非报价单 货源单 车队
     5 非报价单 非货源单 车队
     6 报价单 货源单 车队
     7 报价单 非货源单 车队
     */
    NSInteger _orderState;//订单类型
    
    BOOL _isAgree;
}

@end

@implementation ZTXJieDanBottomView
static const NSInteger tf_Height = 40;
#pragma mark - Intial
- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI {
    
    _dateString = @"";
    _orderState = 0;
    _isAgree = NO;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 140, tf_Height)];
    label.textColor = Dark_TextColor;
    label.text = @"承运数量";
    label.font = font(14);
    label.adjustsFontSizeToFitWidth = YES;
    [self addSubview:label];
    
    self.numberTextField = [[CJTextField alloc] initWithFrame:CGRectMake(140, 20, ScreenW-140-65, tf_Height)];
    self.numberTextField.maxLength = 2;
    self.numberTextField.font = font(14);
    self.numberTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    self.numberTextField.textAlignment = NSTextAlignmentRight;
    self.numberTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.numberTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写每车承运数量" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    self.numberTextField.keyboardType =  UIKeyboardTypePhonePad;
    [self.numberTextField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.numberTextField];
    
    //接单按钮
    UIButton *jiedanButton = [UIButton buttonWithType:UIButtonTypeCustom];
    jiedanButton.tag = 1001;
    cut(jiedanButton, BTN_BORDER_RADIUS);
    [jiedanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    jiedanButton.backgroundColor = Theme_ColorValue;
    [jiedanButton setTitle:@"确定" forState:UIControlStateNormal];
    jiedanButton.titleLabel.font = font(16);
    jiedanButton.frame = CGRectMake(20, CGRectGetMaxY(self.numberTextField.frame), ScreenW - 80, tf_Height);
    [jiedanButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:jiedanButton];

//    //接单协议
    UIButton *xieYiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    xieYiButton.tag = 1100;
    xieYiButton.titleLabel.font = font(12);
    [xieYiButton setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    [xieYiButton setTitle:CJStringWithFormat(@"同意《%@平台与承运方货物运输交易协议》", APP_PlatformName) forState:UIControlStateNormal];
    CGSize bSize = [DCSpeedy dc_calculateTextSizeWithText:CJStringWithFormat(@"同意《%@平台与承运方货物运输交易协议》", APP_PlatformName) WithTextFont:12 WithMaxW:1000];
    xieYiButton.frame = CGRectMake(70, CGRectGetMaxY(jiedanButton.frame) + 5, bSize.width, 30);
    xieYiButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [xieYiButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:xieYiButton.titleLabel.text];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Light_TextColor range:[xieYiButton.titleLabel.text rangeOfString:@"同意"]];
    [attString addAttribute:(NSString*)NSForegroundColorAttributeName value:Theme_ColorValue range:[xieYiButton.titleLabel.text rangeOfString:CJStringWithFormat(@"《%@平台与承运方货物运输交易协议》", APP_PlatformName)]];
    [xieYiButton setAttributedTitle:attString forState:UIControlStateNormal];
    [self addSubview:xieYiButton];

    UIButton *agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    agreeButton.tag = 1101;
    [agreeButton setImage:image(@"gcy_img_home_business_nomal") forState:UIControlStateNormal];
    agreeButton.frame = CGRectMake(xieYiButton.left - 30, xieYiButton.top + 5, 20, 20);
    [agreeButton addTarget:self action:@selector(xieYiButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:agreeButton];
}

- (void)xieYiButtonAction:(UIButton *)button {
    button.selected = !button.selected;
    if (button.isSelected) {
        _isAgree = YES;
        [button setImage:[image(@"gcy_img_home_business_select") imageChangeColor:Theme_ColorValue] forState:UIControlStateNormal];
    } else {
        _isAgree = NO;
        [button setImage:image(@"gcy_img_home_business_nomal") forState:UIControlStateNormal];
    }
   
}

- (void)buttonAction:(UIButton *)button {
    
    WEAKSELF
    
    UITextField *shuLiangTextField = [weakSelf viewWithTag:20];
    UITextField *cheShuTextField = [weakSelf viewWithTag:21];
    UITextField *danJiaTextField = [weakSelf viewWithTag:22];
    
    if (button.tag == 1001) {
        if (_isAgree == NO) {
            [XSInfoView showInfo:@"请先同意交易协议" onView:self.window];
            return;
        }
        if(self.delegate && [self.delegate respondsToSelector:@selector(ZTXProjectDelegateNumbdr:)]){
            [self.delegate ZTXProjectDelegateNumbdr:self.numberTextField.text];
        }
    }
}


- (void)textFieldChanged:(UITextField *)textField {
    if([textField.text floatValue] > 200){
        textField.text = @"200";
    }
}

- (void)setOrderInfoDic:(NSDictionary *)orderInfoDic {
    
    UILabel *numTextLabel = [self viewWithTag:666];
    UILabel *priceTextLabel = [self viewWithTag:888];
    
    UIButton *jieDanBtn = [self viewWithTag:1001];
    UIButton *xieYiButton = [self viewWithTag:1100];
    UIButton *agreeButton = [self viewWithTag:1101];
    
    _orderInfoDic = orderInfoDic;
    
    priceTextLabel.text = [NSString stringWithFormat:@"报价单价(%@)", orderInfoDic[@"isTrunkName"]];
    
    if ([orderInfoDic[@"coalUnit"] integerValue] == 0) {
        numTextLabel.text = @"承运数量(吨)";
    } else if ([orderInfoDic[@"coalUnit"] integerValue] == 1) {
        numTextLabel.text = @"承运数量(方)";
    } else if ([orderInfoDic[@"coalUnit"] integerValue] == 2) {
        numTextLabel.text = @"承运数量(件)";
        UITextField *textField = [self viewWithTag:20];
        textField.textFieldType = CJTextFieldStringTypeNumber;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.maxLength = 10;
    }

    
    //车队司机不能接单 需要车队给其分配
    [jieDanBtn setTitle:@"接单" forState:UIControlStateNormal];
    
    xieYiButton.top = jieDanBtn.bottom + 10;
    agreeButton.top = xieYiButton.top + 5;
}


@end
